﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Eneta.Portal.Forum.Common.Data;
using Eneta.Portal.Forum.Core.Service.Application;
using Microsoft.SharePoint;

namespace Eneta.Portal.Forum.Core.Service.Builders
{
    public abstract class ListBuilder
    {
        // Fields
        protected bool createDefaultValues = false;
        protected string listName;

        // Methods
        protected ListBuilder()
        {
        }

        public abstract void AddFields();
        protected string AddFieldToList(string fieldName, SPFieldType fieldType, bool isRequired)
        {
            string str = string.Empty;
            SPList list = null;
            ForumApplication.Instance.SpWeb.AllowUnsafeUpdates = true;
            try
            {
                if (this.ListExists)
                {
                    list = ForumApplication.Instance.SpWeb.Lists[this.listName];
                    str = list.Fields[fieldName].ToString();
                }
            }
            catch
            {
                try
                {
                    return list.Fields.Add(fieldName, fieldType, isRequired);
                }
                catch (Exception)
                {
                    return str;
                }
            }
            return str;
        }

        protected void AddListValues(string[] values)
        {
            SharePointListItem newItem = new SharePointListItem(0, values);
            new SharePointListProvider(ForumApplication.Instance.SpWeb).AddListItem(this.listName, newItem);
        }

        public abstract void AddSampleData();
        public void CreateList()
        {
            if (!this.ListExists)
            {
                this.InternalCreateList();
            }
        }

        public void HideList()
        {
            ForumApplication.Instance.SpWeb.AllowUnsafeUpdates = true;
            SPList list = ForumApplication.Instance.SpWeb.Lists[this.listName];
            list.Hidden = true;
            list.Update();
        }

        private Guid InternalCreateList()
        {
            ForumApplication.Instance.SpWeb.AllowUnsafeUpdates = true;
            this.createDefaultValues = true;
            return ForumApplication.Instance.SpWeb.Lists.Add(this.listName, string.Empty, (SPListTemplateType) 100);
        }

        // Properties
        public bool CreateDefaultValues
        {
            get
            {
                return this.createDefaultValues;
            }
        }

        public bool ListExists
        {
            get
            {
                bool flag = false;
                try
                {
                    Console.WriteLine(ForumApplication.Instance.SpWeb.Lists[this.listName]);
                    flag = true;
                }
                catch (Exception)
                {
                }
                return flag;
            }
        }
    }
}
